package fr.afcepf.atod18.ws.currencies;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

import org.springframework.beans.factory.annotation.Autowired;

import fr.afcepf.atod18.ws.currencies.dao.CountryPersistence;
import fr.afcepf.atod18.ws.currencies.dao.CurrencyPersistence;
import fr.afcepf.atod18.ws.currencies.dao.ICountryDAO;
import fr.afcepf.atod18.ws.currencies.dao.ICurrencyDAO;

@WebService
public class ServiceCurrencies implements IServiceCurrencies {

    @Autowired
    private ICurrencyDAO currencyDao;
    @Autowired
    private ICountryDAO countryDao;

    @WebMethod(operationName="getAllCurrenciesEuroBased_WS")
    public List<CurrencyDTO> getAllCurrenciesEuroBased() {
        List<CurrencyPersistence> lcp = currencyDao.getAll();
        List<CurrencyDTO> returnList = new ArrayList<CurrencyDTO>();
        for (CurrencyPersistence cp : lcp) {
            returnList.add(new CurrencyDTO(cp.getName(), cp.getCountries().get(0)
                    .getCountryName(), cp.getRate()));
        }
        return returnList;
    }

    @Override
    @WebMethod(operationName="getChangeRateForCurrency_WS")
    public List<CurrencyDTO> getChangeRateForCurrency(
            @WebParam(name = "CurrencyName") String paramCurrencyName) {
        List<CurrencyPersistence> lcp = currencyDao
                .getCurrencyByName(paramCurrencyName);
        List<CurrencyDTO> returnList = new ArrayList<CurrencyDTO>();
        for (CurrencyPersistence cp : lcp) {
            returnList.add(new CurrencyDTO(cp.getName(), cp.getCountries().get(0)
                    .getCountryName(), cp.getRate()));
        }
        return returnList;
    }

    @Override
    @WebMethod(operationName="getChangeRateForCountry_WS")
    public List<CurrencyDTO> getChangeRateForCountry(
            @WebParam(name = "CountryName") String paramCountryName) {
        List<CountryPersistence> lc = countryDao
                .getCountryByName(paramCountryName);
        List<CurrencyDTO> returnList = new ArrayList<CurrencyDTO>();
        for (CountryPersistence c : lc) {
            returnList.add(new CurrencyDTO(c.getDevise().getName(), c
                    .getCountryName(), c.getDevise().getRate()));
        }
        return returnList;
    }

    @Override
    @WebMethod(operationName="getChange_WS")
    public double getChange(@WebParam(name = "Amount") double paramAmount,
            @WebParam(name = "CurrencyNameIn") String paramInCurrency,
            @WebParam(name = "CurrencyNameOut") String paramOutCurrency) {
        List<CurrencyPersistence> lin = currencyDao
                .getCurrencyByName(paramInCurrency);
        List<CurrencyPersistence> lout = currencyDao
                .getCurrencyByName(paramOutCurrency);
        if (lin != null && lin.size() > 0 && lin.get(0) != null && lout != null
                && lout.size() > 0 && lout.get(0) != null
                && lin.get(0).getRate() != 0 && lout.get(0).getRate() != 0) {
            return paramAmount * lin.get(0).getRate() / lout.get(0).getRate();
        }
        return -1;
    }

}
